package pers.whj.rpc.serialize;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Output;
import pers.whj.rpc.common.Config;

/**
 * 序列化为字节数组
 */
public class EncoderImpl implements Encoder {
    static Kryo kryo = new Kryo();

    /**
     * 对象转为二进制数组
     *
     * @param obj 对象
     * @return 二进制数组
     */
    @Override
    public byte[] encode(Object obj) {
        byte[] buffer = new byte[Config.MAX_TRANSPORT_BYTE];
        Output out = new Output(buffer);
        kryo.writeClassAndObject(out, obj);
        byte[] result = out.toBytes();
        out.close();
        return result;
    }
}
